package connectors;


import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import agency_terminal.data_types.OperationRequest;
import agency_terminal.data_types.OperationResponse;
import agency_terminal.enums.OperationResults;

public class Agency {
	private String host = "localhost";
	private Integer port = 15000;
	private Integer timeOut = 100000;
		

	public Agency(){		
	}
	
	public Agency(String host, Integer port, Integer timeOut) {
		super();
		this.host = host;
		this.port = port;
		this.timeOut = timeOut;
	}

	public OperationResponse executeOperation(OperationRequest operationRequest){
		OperationResponse operationResponse = null;
		
		try {
			Socket socket = new Socket(host, port);
			socket.setSoTimeout(timeOut);
					
			ObjectOutputStream objOut = new ObjectOutputStream(socket.getOutputStream());
			ObjectInputStream objIn = new ObjectInputStream(socket.getInputStream());
			
			//se manda el objeto
			objOut.writeObject(operationRequest);
			objOut.flush();
			
			//se espera respuesta
			operationResponse = (OperationResponse) objIn.readObject();
			
			if (operationResponse == null){
				operationResponse = new OperationResponse(OperationResults.error, "Error al comunicarse con la agencia", null);
			}
			
			objOut.close();
			objIn.close();
			socket.close();
			
		} catch (UnknownHostException e) {			
			e.printStackTrace();
			operationResponse = new OperationResponse(OperationResults.error, "Error al comunicarse con la agencia", null);
		} catch (IOException e) {
			e.printStackTrace();
			operationResponse = new OperationResponse(OperationResults.error, "Error al comunicarse con la agencia", null);
		} catch (ClassNotFoundException e) {			
			e.printStackTrace();
			operationResponse = new OperationResponse(OperationResults.error, "Error al comunicarse con la agencia", null);			
		}
		
		return operationResponse;					
	}
	
	
	
/* TEST
	
	public static void main(String[] args) {
		Agency agency = new Agency();
		
		agency.testLoginUser();
		agency.testCreateTicket();
		agency.testCancelTicket();		
	}
	
	private void testLoginUser(){
		Data data = new DataUserRequest();
		((DataUserRequest) data).setUser("pablo");
		((DataUserRequest) data).setPassword("secreta");		
		
		OperationRequest operacionRequest = new OperationRequest(OperationTypes.userLogin, data, "127.0.0.0");
		OperationResponse operationResponse = this.executeOperation(operacionRequest);
		System.out.println("Recibimos la respuesta del servidor: " + operationResponse.getResult() + " - " + operationResponse.getResultMessage());
	}
	
	private void testCreateTicket(){
		Data dataRequest = new DataTicketRequest();
		((DataTicketRequest) dataRequest).setCarRegistration("SAF-2011");	
		
		OperationRequest operacionRequest = new OperationRequest(OperationTypes.ticketCreate, dataRequest, "127.0.0.0");
		OperationResponse operationResponse = this.executeOperation(operacionRequest);
		System.out.println("Recibimos la respuesta del servidor: " + operationResponse.getResult() + " - " + operationResponse.getResultMessage());

		DataTicketResponse dataResponse = (DataTicketResponse) operationResponse.getData();
		System.out.println("El valor del ticket es: " + dataResponse.getValue());								
	}	
	
	private void testCancelTicket(){
		Data dataRequest = new DataTicketRequest();
		((DataTicketRequest) dataRequest).setId(1000);	
		
		OperationRequest operacionRequest = new OperationRequest(OperationTypes.ticketCancel, dataRequest, "127.0.0.0");
		OperationResponse operationResponse = this.executeOperation(operacionRequest);
		System.out.println("Recibimos la respuesta del servidor: " + operationResponse.getResult() + " - " + operationResponse.getResultMessage());

		DataTicketResponse dataResponse = (DataTicketResponse) operationResponse.getData();
		System.out.println("El id de anulacion es: " + dataResponse.getIdCancellation());								
	}	
*/
}
